﻿<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="screen-orientation" content="portrait" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
    <meta content="black" name="apple-mobile-web-app-status-bar-style" />
    <meta content="telephone=no" name="format-detection" />
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <title>流星</title>
    <script type="text/javascript" src="js/jquery-3.4.1.min.js"></script>
    <script type="text/javascript" src="js/vue.js"></script>
    <style>
        .item {
            width: 200px;
            height: 60px;
            line-height: 60px;
            text-align: center;
            background-color: #f0f0f0;
        }
    </style>
</head>
<body>
    <div id="app">
        <a href="index.html"> 返回首页</a>
        <div class="group-box">
            <div class="form-inline showchat" style="width: 450px;height: 420px;background-color: #808080;color: #ffffff;padding: 10px;overflow:auto;">
                <div v-for="info,index in datalist">
                    <div style="float: left;" v-if="info.name!=user">
                        <div style="position: relative;min-height: 50px;width: 350px;">
                            <div style="position: absolute;left: 5px;top:5px;border-radius: 20px;width: 40px;height: 40px;line-height: 40px;text-align: center;background-color: #000F1F;">{{info.name}}</div>
                            <div class="xdiv" style="padding: 10px;position: absolute;left: 50px;top:5px;background-color: #000F1F;border-radius: 5px;">
                                {{info.msg}}
                            </div>
                        </div>
                    </div>
                    <div style="float:right;" v-if="info.name==user">
                        <div style="position: relative;min-height: 50px;width: 350px;">
                            <div class="xdiv" style="padding: 10px;position: absolute;right: 50px;top:5px;background-color: #0d1f00;border-radius: 5px;">
                                {{info.msg}}
                            </div>
                            <div style="position: absolute;right: 5px;top:5px;border-radius: 20px;width: 40px;height: 40px;line-height: 40px;text-align: center;background-color: #000F1F;">{{info.name}}</div>
                        </div>
                    </div>

                </div>
            </div>
            <div class="form-inline">
                <textarea style="width: 450px;" rows="4" v-model="content"></textarea>
                <div>
                    <button type="button" style="margin-top: 5px;" v-on:click="send">发送</button>
                </div>
            </div>
        </div>

    </div>
</body>
</html>

<script type="text/javascript">

    function wsload(userid) {
        var url = "ws://" + location.host + '/Message.ashx?code=Iamfromchina&user=WS' + userid;
        window.wsMsg = new WebSocket(url);
        console.log(window.wsMsg);
        window.wsMsg.onopen = function () { console.log("ws", "已经连接"); }
        window.wsMsg.onmessage = function (evt) {
            console.log('接收消息', evt.data);
            this.datalist.push(evt);
        }
        window.wsMsg.onerror = function (evt) { console.log("ws", evt); }
        window.wsMsg.onclose = function () { console.log("ws", "连接断开"); }
    }


    $(function () {
        var app = new Vue({
            el: '#app',
            data: {
                userid: 2,
                aimid: 1,
                user:"流星",
                datalist: [],
                content:""
            },
            methods: {
                send: function () {
                    if (window.wsMsg.readyState == WebSocket.OPEN) {
                        var info = {
                            name: this.user,
                            msg: this.content
                        };
                        this.datalist.push(info);
                        var msg = JSON.stringify({
                            user: "WS" + this.aimid,
                            msg: info
                        });
                        window.wsMsg.send(msg);
                        return true;
                    }
                    else {
                        alert("消息发送失败，WS连接关闭!");
                        return false;
                    }
                },
                wsload: function () {
                    var that = this;
                    var url = "ws://" + location.host + '/Message.ashx?code=Iamfromchina&user=WS' + this.userid;
                    window.wsMsg = new WebSocket(url);
                    console.log(window.wsMsg);
                    window.wsMsg.onopen = function () { console.log("ws", "已经连接"); }
                    window.wsMsg.onmessage = function (evt) {
                        var info = JSON.parse(evt.data);
                        console.log('接收消息', evt.data);
                        console.log(that.datalist)
                        that.datalist.push(info);
                    }
                    window.wsMsg.onerror = function (evt) { console.log("ws", evt); }
                    window.wsMsg.onclose = function () { console.log("ws", "连接断开"); }
                }
            },
            created: function () {
                this.wsload();
            },
        });
    });
</script>